package com.jz.yanshe.ui;

import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.exception.BusinessException;
import com.jz.jd.rest.response.BaseResponse;
import com.jz.jd.rest.response.LoginResponse;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.util.PreferencesHelper;
import com.jz.yanshe.base.BaseActivity;
import jz.com.feierman.R;


/**
 * Created by Administrator on 2018/3/12.
 */

public class LoginActivity extends BaseActivity {

    @BindView(R.id.text_phone)
    TextView text_phone;

    @BindView(R.id.text_yzm)
    TextView text_yzm;
    @BindView(R.id.invite_linear)
    LinearLayout invite_linear;

    @BindView(R.id.text_invite_code)
    EditText text_invite_code;

    String access_token;
    String openid;
    String headimgurl;
    String sex;
    String nickname;


    @Override
    protected void initViews() {

    }

    @Override
    protected void initData() {
        access_token = getIntent().getStringExtra("access_token");
        openid        = getIntent().getStringExtra("openid");
        nickname = getIntent().getStringExtra("nickname");
        headimgurl = getIntent().getStringExtra("headimgurl");
        sex = getIntent().getStringExtra("sex");
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_login;
    }

    @OnClick(R.id.text_yzm_huoqu)
    void text_yzm_huoqu() {
        getYZM();
    }

    @OnClick(R.id.text_login)
    void text_login() {
        login();
    }

    private void getYZM() {
        RestProxy.getInstance().getYZM(text_phone.getText().toString(), "4", new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Toast.makeText(LoginActivity.this, baseResponse.message, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void login() {
        RestProxy.getInstance().login(access_token, openid, nickname, headimgurl, text_phone.getText().toString(), sex, "4", text_yzm.getText().toString(), text_invite_code.getText().toString(), new Observer<LoginResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(LoginResponse loginResponse) {
                PreferencesHelper.getInstance().set(LoginActivity.this, "token", loginResponse.map.user_token);
//                startActivity(HomeActivity.class);
                finish();
            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof BusinessException) {
                    if (((BusinessException) e).errorCode == 8) {
                        invite_linear.setVisibility(View.VISIBLE);
                    }
                    Toast.makeText(LoginActivity.this, ((BusinessException) e).errorDescription, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

}
